package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.DepartmentDTO;
import cn.xn.hrms.api.dto.req.DepartmentReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.infrastructure.dataobject.PositionDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.DepartmentService;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.domain.service.PositionService;
import cn.xn.hrms.infrastructure.dao.DepartmentDao;

/**
 * @author ysz
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private PersonalService personalService;
    @Resource
    private PositionService positionService;

    @Override
    public PageInfo<DepartmentDTO> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DepartmentDTO> departmentDTOS = departmentDao.selectAll();
        return new PageInfo<>(departmentDTOS);
    }

    @Override
    public List<DepartmentDTO> all() {
        return departmentDao.selectAll();
    }

    @Override
    public PageInfo<DepartmentDTO> search(String departmentName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DepartmentDTO> departmentDTOS = departmentDao.selectAllByDepartmentNameLike(departmentName);
        return new PageInfo<>(departmentDTOS);
    }

    @Override
    public DepartmentDTO find(int id) {
        DepartmentDTO departmentDTO = departmentDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(departmentDTO)) {
            throw new BaseException(ResultEnum.DEPARTMENT_NOT_EXIST);
        }
        return departmentDTO;
    }

    @Override
    public void insert(DepartmentReqDTO record) {

        DepartmentDTO findDepartment = departmentDao.findOneByDepartmentName(record.getDepartmentName());
        if (ObjectUtil.isNotNull(findDepartment)) {
            throw new BaseException(ResultEnum.DEPARTMENT_EXISTS);
        }
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(record, departmentDTO);
        departmentDTO.setIsDeleted(0);
        departmentDao.insertSelective(departmentDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        //校验该部门有没有被绑定，绑定了无法删除
        final List<PersonalDO> personalAll = personalService.All();
        personalAll.forEach(personalVo -> idList.forEach(id -> {
            if (id.equals(personalVo.getDepartmentId())) {
                throw new BaseException(ResultEnum.DEPARTMENT_IS_BIND);
            }
        }));
        final List<PositionDO> positionAll = positionService.all();
        positionAll.forEach(positionVo -> idList.forEach(id -> {
            if (id.equals(positionVo.getDepartmentId())) {
                throw new BaseException(ResultEnum.POSITION_IS_BIND);
            }
        }));
        departmentDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, DepartmentReqDTO record) {
        DepartmentDTO departmentDTO = departmentDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(departmentDTO)) {
            throw new BaseException(ResultEnum.DEPARTMENT_NOT_EXIST);
        }
        BeanUtils.copyProperties(record, departmentDTO);
        departmentDTO.setDepartmentId(id);
        departmentDao.updateByPrimaryKeySelective(departmentDTO);
    }

}
